//
//  PreferenceController.m
//  SimpleDraw
//
//  Created by David Hatch on 12/6/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "PreferenceController.h"

NSString * const TESDBgColorKey = @"TE_SDBgColorKey";
NSString * const TESDFgColorKey = @"TE_SDFgColorKey";
NSString * const TESDPenSizeKey = @"TE_SDSizeKey";
NSString * const TESDBgColorChange = @"TE_SDBgChange";

@implementation PreferenceController
-(id)init
{
	if(![super initWithWindowNibName:@"Inspector"])
		return nil;
	return self;
}

-(void)windowDidLoad
{
	[fgColor setColor:[self fgColor]];
	[bgColor setColor:[self bgColor]];
	[radius setFloatValue:[self penSize]];
	[radiusText setIntValue:[self penSize]];
	
}

#pragma mark Setters
-(IBAction)bgColorChanged:(id)sender
{
	NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
	NSData *colorAsData = [NSKeyedArchiver archivedDataWithRootObject:[bgColor color]];
	[d setObject:colorAsData forKey:TESDBgColorKey];
	NSDictionary *info = [NSDictionary dictionaryWithObject:[bgColor color] forKey:@"color"];
	NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter];
	[noteCenter postNotificationName:TESDBgColorChange object:self userInfo:info];
}

-(IBAction)fgColorChanged:(id)sender
{
	NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
	NSData *colorAsData = [NSKeyedArchiver archivedDataWithRootObject:[fgColor color]];
	[d setObject:colorAsData forKey:TESDFgColorKey];
}

-(IBAction)radiusChanged:(id)sender
{
	NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
	[d setFloat:[radius floatValue] forKey:TESDPenSizeKey];
	[radiusText setIntValue:[d floatForKey:TESDPenSizeKey]];
}

#pragma mark Accessors
-(NSColor *)bgColor
{
	NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
	NSColor * c = [NSKeyedUnarchiver unarchiveObjectWithData:[d valueForKey:TESDBgColorKey]];
	NSLog(@"bgColor is %@", c);
	return c;
}

-(NSColor *)fgColor
{
	NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
	NSColor * c = [NSKeyedUnarchiver unarchiveObjectWithData:[d valueForKey:TESDFgColorKey]];
		NSLog(@"fgColor is %@", c);
	return c;
}

- (float)penSize
{
	float s = [[NSUserDefaults standardUserDefaults] integerForKey:TESDPenSizeKey];
	return s;
}

@end
